package com.wangxinyuchuan.demo.singleton;

/**
 * @author wangxinyuchuan
 * @date 2023/4/14 12:31 PM
 */
public class Triple {

    private int id;

    // 开始时默认为null，一旦启动程序，triples数组中的所有元素都会被初始化
    private static Triple[] triples = new Triple[]{
            new Triple(0),
            new Triple(1),
            new Triple(2),
    };

    private Triple(int id) {
        System.out.println("The instance " + id + " is Created!");
        this.id = id;
    }

    public static Triple getInstance(int id) {
        return triples[id];
    }

    @Override
    public String toString() {
        return "Triple{" +
                "id=" + id +
                '}';
    }

    public static void main(String[] args) {
        Triple instance1 = Triple.getInstance(0);
        Triple instance2 = Triple.getInstance(1);
        Triple instance3 = Triple.getInstance(2);
        System.out.println(instance1);
        System.out.println(instance2);
        System.out.println(instance3);
    }
}
